are regular expressions even appropriate for this problem? I'm trying to extract blocks of text surrounded by 'raw' curly braces. so anything surrounded by "" or **,"",~~,``, should be ignored entirely. sample text:
{
"text1"
'text2'
*text3*
~text4~
`text5`
}
{
"half broken1}{"
*half broken2}{*
'half broken3}{'
~half broken4}{~
`half broken5}{`
}
{
"{nested text1}"
'{nested text2}'
~{nested text3}~
*{nested text4}*
`{nested text5}`
}
{
"text1"
{
`text2`
}
}
expected output:
[
"\"text1\"
'text2'
*text3*
~text4~
`text5`",
"\"half broken1}{\"
*half broken2}{*
'half broken3}{'
~half broken4}{~
`half broken5}{`",
"\"{nested text1}\"
'{nested text2}'
~{nested text3}~
*{nested text4}*
`{nested text5}`",
"\"text1\"
{
`text2`
}",
"`text2`"
]
I can target curly braces with:
/(?<=\{)[^}]*(?=\})/gm
but that eats the first } facing brace it finds to match. I have tried a few other strings, but so far - I don't know how to ignore {"}"} the first brace in this case.
(using js syntax for regex)
https://regex101.com/r/EODDXL/1
(?<=\{)(?:"[^"]*"|'[^']*'|\*[^*]*\*|~[^~]*~|`[^`]*`|{[^}]*}|[^}])*?(?=\})
That's starting with what you had, but includes ignoring " followed by 0 or more [^"] followed by a ". Then repeat this pattern for ', *, ~, ` and {}. This captures the outer groups only. I'm not sure how exactly you'd get the expected output at the end, but this may get you in the right direction.